<?php
/* $Id: types.docs.php,JOP
/**
 * @package Issue-Tracker
 * @subpackage Docs
 */
if (strpos($_SERVER['PHP_SELF'],basename(__FILE__)) !== false) {
  print "Direct module access forbidden.".basename(__FILE__);
  exit;
}

if(is_admin()){
  if ($_GET['subaction'] == "delete"
  and !empty($_GET['id'])) {
    if ($_POST['confirm'] == "true") {
        $sql  = "DELETE FROM z_doc_types ";
        $sql .= "WHERE tid='".$_GET['id']."'";
        $dbi->query($sql);
        redirect("?module=docs&action=types");
    } else {
      $smarty->display("docs/types/delete.tpl");
    }
  } else if ($_GET['subaction'] == "new") {
    if ($_POST['commit'] == "true") {
      if (empty($_POST['text'])) {
        push_error("Text can not be empty.");
      } else {
        $sql  = "SELECT tid ";
        $sql .= "FROM z_doc_types ";
        $sql .= "WHERE LOWER(text) = LOWER('".trim($_POST['text'])."')";
        $tid = $dbi->fetch_one($sql);
        if (!empty($tid)) {
          push_error("Document-Type already exists.");
        } else {

          $insert['text']       = $_POST['text'];
          $dbi->insert("z_doc_types",$insert);
          redirect("?module=docs&action=types");
        }
      }
    }
    
    if (empty($_POST['commit']) or errors()) {
      $smarty->display("docs/types/new.tpl");
    }
  } else if ($_GET['subaction'] == "edit" and !empty($_GET['id'])) {
    if ($_POST['commit'] == "true") {
      if (empty($_POST['text'])) {
        push_error("Document Type can not be empty.");
      } else {
        $sql  = "SELECT text ";
        $sql .= "FROM z_doc_types ";
        $sql .= "WHERE LOWER(text) = LOWER('".trim($_POST['text'])."')";
        $tid = $dbi->fetch_one($sql);
        if (empty($tid) or $tid == $_GET['id']) {

          $update['text']       = $_POST['text'];
          $dbi->update("z_doc_types",$update,"WHERE tid='".$_GET['id']."'");
          redirect("?module=docs&action=types");
        } else {
          push_error("Document-Type already exists.");
        }
      }
    } 

    if (empty($_POST['commit']) or errors()) {
       $sql  = "SELECT * ";
       $sql .= "FROM z_doc_types ";
       $sql .= "WHERE tid ='".$_GET['id']."'";
       $type = $dbi->fetch_row($sql,"array");
       $smarty->assign('type',$type);
       $smarty->display("docs/types/edit.tpl");
       }
  } else {
    $links[] = array(
      "txt" => $lg->mm_add,
      "url" => "?module=docs&action=types&subaction=new",
      "img" => $_ENV['imgs']['user']
    );

    $sql  = "SELECT * ";
    $sql .= "FROM z_doc_types ";
    $sql .= "ORDER BY tid";
    $types = $dbi->fetch_all($sql,"array");

    $smarty->assign('types',$types);
    $smarty->display("docs/types.tpl");
  }
} else {
  access_denied();
}
?>